/**
 * 在钉钉/uni 小程序环境中通过 auth code 获取后台的 dingding uid
 * 优先使用 dd.httpRequest（钉钉小程序），没有时回退到 uni.request
 * 返回 Promise，resolve 后端返回的 data
 */
export function getDingDingUid(code) {
  return new Promise((resolve, reject) => {
    const url = 'https://bsc-gateway.sonline.hzmantu.com/bsc-store-activity/dingding/getDingDingUid'
    const payload = { code }

    if (typeof dd !== 'undefined' && typeof dd.httpRequest === 'function') {
      dd.httpRequest({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: (res) => { resolve(res && res.data ? res.data : res) },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof uni !== 'undefined' && typeof uni.request === 'function') {
      uni.request({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        header: { 'Content-Type': 'application/json; charset=UTF-8' },
        success: (res) => {
          // uni.request 回调结构: { statusCode, data }
          if (res && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => { reject(err) }
      })
      return
    }

    // 浏览器环境可选择使用 fetch
    if (typeof fetch === 'function') {
      fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      }).then(r => r.json()).then(resolve).catch(reject)
      return
    }

    reject(new Error('No HTTP request method available'))
  })
}

/**
 * 查询宣言接口
 * payload: { workNumberList: [123, ...] }
 */
export function searchDeclaration(workNumberList) {
  return new Promise((resolve, reject) => {
    const url = 'https://bsc-gateway.sonline.hzmantu.com/bsc-store-activity/partner/searchDeclaration'
    const payload = { workNumberList: Array.isArray(workNumberList) ? workNumberList : [] }

    if (typeof dd !== 'undefined' && typeof dd.httpRequest === 'function') {
      dd.httpRequest({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: (res) => { resolve(res && res.data ? res.data : res) },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof uni !== 'undefined' && typeof uni.request === 'function') {
      uni.request({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        header: { 'Content-Type': 'application/json; charset=UTF-8' },
        success: (res) => {
          if (res && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof fetch === 'function') {
      fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      }).then(r => r.json()).then(resolve).catch(reject)
      return
    }

    reject(new Error('No HTTP request method available'))
  })
}

/**
 * 保存/发布宣言接口
 * payload: { publishDeclarationItemParamList: [{ workNumber, declaration }, ...] }
 */
export function publishDeclaration(publishDeclarationItemParamList) {
  return new Promise((resolve, reject) => {
    const url = 'https://bsc-gateway.sonline.hzmantu.com/bsc-store-activity/partner/publishDeclaration'
    const payload = { publishDeclarationItemParamList: Array.isArray(publishDeclarationItemParamList) ? publishDeclarationItemParamList : [] }

    if (typeof dd !== 'undefined' && typeof dd.httpRequest === 'function') {
      dd.httpRequest({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: (res) => { resolve(res && res.data ? res.data : res) },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof uni !== 'undefined' && typeof uni.request === 'function') {
      uni.request({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        header: { 'Content-Type': 'application/json; charset=UTF-8' },
        success: (res) => {
          if (res && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof fetch === 'function') {
      fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      }).then(r => r.json()).then(resolve).catch(reject)
      return
    }

    reject(new Error('No HTTP request method available'))
  })
}

// 新增：根据工号列表查询参赛人员表格数据
export function searchAnswerInfo(workNumberList) {
  return new Promise((resolve, reject) => {
    const url = 'https://bsc-gateway.sonline.hzmantu.com/bsc-store-activity/partner/searchAnswerInfo'
    const payload = { workNumberList: Array.isArray(workNumberList) ? workNumberList : [] }

    if (typeof dd !== 'undefined' && typeof dd.httpRequest === 'function') {
      dd.httpRequest({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: (res) => { resolve(res && res.data ? res.data : res) },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof uni !== 'undefined' && typeof uni.request === 'function') {
      uni.request({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        header: { 'Content-Type': 'application/json; charset=UTF-8' },
        success: (res) => {
          if (res && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof fetch === 'function') {
      fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      }).then(r => r.json()).then(resolve).catch(reject)
      return
    }

    reject(new Error('No HTTP request method available'))
  })
}

// 新增：根据工号查询个人主页所有信息
export function searchPersonalInfo(workNumber) {
  return new Promise((resolve, reject) => {
    const url = 'https://bsc-gateway.sonline.hzmantu.com/bsc-store-activity/partner/personalAnswerInfo'
    // const url = 'https://bsc-gateway.spre.hzmantu.com/bsc-store-activity/partner/personalAnswerInfo'
    const payload = {workNumber: Number(workNumber)}

    if (typeof dd !== 'undefined' && typeof dd.httpRequest === 'function') {
      dd.httpRequest({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: (res) => { resolve(res && res.data ? res.data : res) },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof uni !== 'undefined' && typeof uni.request === 'function') {
      uni.request({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        header: { 'Content-Type': 'application/json; charset=UTF-8' },
        success: (res) => {
          if (res && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof fetch === 'function') {
      fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      }).then(r => r.json()).then(resolve).catch(reject)
      return
    }

    reject(new Error('No HTTP request method available'))
  })
}

/**
 * 根据工号、赛道岗位、场景查询对应排行榜信息
 * @param {number} workNumber 据工号（只有参数人员用）
 * @param {string} track 赛道岗位（蓝标-服务管家赛场、蓝标-造型顾问赛场、蓝标-摄影师猜测、蓝标-看片师赛场、大师-服务管家赛场、大师-造型顾问赛场、大师-摄影师猜测、大师-看片师赛场、云端-修图师赛场）它只有非参赛人员，点击首页按钮的时候用
 * @param {string} scene 场景（PERSONAL：个人榜单   TEAM：团队榜单）
 * @returns 
 */
export function searchTeamAnswerInfo(workNumber, track, scene = 'PERSONAL') {
  return new Promise((resolve, reject) => {
    const url = 'https://bsc-gateway.sonline.hzmantu.com/bsc-store-activity/partner/teamAnswerInfo'
    // const url = 'https://bsc-gateway.spre.hzmantu.com/bsc-store-activity/partner/teamAnswerInfo'
    const payload = { workNumber, track, scene }

    if (typeof dd !== 'undefined' && typeof dd.httpRequest === 'function') {
      dd.httpRequest({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: (res) => { resolve(res && res.data ? res.data : res) },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof uni !== 'undefined' && typeof uni.request === 'function') {
      uni.request({
        url,
        method: 'POST',
        data: JSON.stringify(payload),
        header: { 'Content-Type': 'application/json; charset=UTF-8' },
        success: (res) => {
          if (res && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => { reject(err) }
      })
      return
    }

    if (typeof fetch === 'function') {
      fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      }).then(r => r.json()).then(resolve).catch(reject)
      return
    }

    reject(new Error('No HTTP request method available'))
  })
}

export default { getDingDingUid, searchAnswerInfo, searchPersonalInfo, searchTeamAnswerInfo }